//
//  theOrderView.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/6/22.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "theOrderView.h"

@interface theOrderView ()<UITableViewDelegate , UITableViewDataSource>

Copy void(^selectedHandle)(NSString *str);
Strong UITableView *tableView;
Strong NSMutableArray *dataArray; /**<*/
Assign int choose; /**<*/
Strong UIView *headView;
Strong UIView *footView;

@end

@implementation theOrderView

- (instancetype)initWithFrame:(CGRect)frame selectedHandle:(void (^)(NSString *))selectedHandle{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        _selectedHandle = selectedHandle;
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backButton.backgroundColor = [UIColor blackColor];
        backButton.alpha = .6;
        backButton.titleLabel.font = DEF_FontSize_13;
        [backButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        [self addSubview:self.tableView];
        self.choose = 3;
    }
    return self;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(50 / WIDTH_5S_SCALE, (SCREEN_HEIGHT - 300) / 2, SCREEN_WIDTH - 100 / WIDTH_5S_SCALE, 90 / WIDTH_5S_SCALE + 88) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footView;
        _tableView.tableHeaderView = self.headView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = getColor(@"ffffff");
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        _tableView.layer.masksToBounds = YES;
        _tableView.layer.cornerRadius = 5;
    }
    return _tableView;
}

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 100 / WIDTH_5S_SCALE, 30 / WIDTH_5S_SCALE)];
        _headView.backgroundColor = getColor(mainColor);
        [_headView addSubview:({
            UILabel *titleLabel  = [[UILabel alloc]initWithFrame:self.headView.frame];
            titleLabel.font = DEF_FontSize_16;
            titleLabel.textColor = getColor(@"ffffff");
            titleLabel.text = @"订单";
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel;
        })];
    }
    return _headView;
}

- (UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 100 / WIDTH_5S_SCALE, 60 / WIDTH_5S_SCALE)];
        [_footView addSubview:({
            UIButton *typeButton = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 100 / WIDTH_5S_SCALE - 40 / WIDTH_5S_SCALE) / 2, 20, 40 / WIDTH_5S_SCALE, 20 / WIDTH_5S_SCALE)];
            typeButton.titleLabel.font = DEF_FontSize_14;
            [typeButton setTitle:@"确定" forState:UIControlStateNormal];
            [typeButton setTitleColor:getColor(@"ffffff") forState:UIControlStateNormal];
            typeButton.backgroundColor = getColor(mainColor);
            typeButton.layer.masksToBounds = YES;
            typeButton.layer.cornerRadius = 5;
            [typeButton addTarget:self action:@selector(typeButtonClick) forControlEvents:UIControlEventTouchUpInside];
            typeButton;
        })];
    }
    return _footView;
}

- (void)typeButtonClick{
    if (self.choose == 3) {
        [self makeToast:@"至少选择一项" duration:2 position:CSToastPositionCenter];
        return;
    }
    [self dismiss];
    if (_selectedHandle) {
        _selectedHandle([NSString stringWithFormat:@"%d",self.choose]);
    }
}


#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    [cell.contentView addSubview:({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(24, 0, SCREEN_WIDTH-60, 44)];
        label.font = DEF_FontSize_14;
        label.text = self.dataArray[indexPath.row];
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 100 / WIDTH_5S_SCALE - 30 / WIDTH_5S_SCALE - 20 / WIDTH_5S_SCALE, 0, 30 / WIDTH_5S_SCALE, 44)];
        [btn setImage:[UIImage imageNamed:@"rect"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"ok2"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(timeScoreEvent:) forControlEvents:UIControlEventTouchUpInside];
//        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        btn.tag = 2000 + indexPath.row;
        if (self.choose == indexPath.row) {
            btn.selected = YES;
        }
        [view addSubview:label];
        [view addSubview:btn];
        view;
    })];
    return cell;
}

- (void)timeScoreEvent:(UIButton *)sender{
    self.choose = (int)sender.tag - 2000;
    [self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.choose = (int)indexPath.row;
    [self.tableView reloadData];
}

//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - data

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray  = @[
                        @"进店消费",
                        @"等待发货"
                        ].mutableCopy;
    }
    return _dataArray;
}


@end
